package com.title6;

import javafx.print.Collation;

import java.util.*;
import java.util.concurrent.Callable;

/**
 * @Author milian
 * @Date 2021/8/6 0006 20:47
 */
public class jackpot implements Runnable {
    private int[] arr ={85, 100, 60, 10, 50, 20, 22,8};
    private static final Object obj = new Object();
//    将int类型数组转化成Integer数组
    private Integer[] integer = Arrays.stream(arr).boxed().toArray(Integer[]::new);
//    将Integer数组转化成ArrayList集合
    private List<Integer> arrayList = new ArrayList(Arrays.asList(integer));
    public int sum1=0;
    public int sum2=0;
    @Override
    public void run() {
        while (true) {
            synchronized (jackpot.class) {
                if (arrayList.size() == 0) {
                    break;
                }else if ("抽奖箱1".equals(Thread.currentThread().getName())) {
                    sum1+= mySynchronized();
                }else if ("抽奖箱2".equals(Thread.currentThread().getName())) {
                    sum2+=  mySynchronized();
                }
            }
        }
        System.out.println(sum1);
        System.out.println(sum2);
    }


    private synchronized int mySynchronized() {
            int index = 0;
            int bonus =0;
            if (arrayList.size()!=0) {
                index = getIndex(arrayList.size());
                bonus = arrayList.get(index);
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"抽中了" + bonus + "元奖金");
            arrayList.remove(index);
            return bonus;
    }

    private int getIndex(int size) {
        Random random = new Random();
        int index = 0;
        if (size>1) {
            index = random.nextInt(size);
        }else {
            return index;
        }
        return index;
    }


}
